<?php

if (!isset($website) ) { header('HTTP/1.1 404 Not Found'); die; }

    if ( isset($_GET["success"]) ) $errors = $lang["report_successfull"] ;
   
    if ( isset($_GET["user"]) ) {
	   $ReportedPlayer = safeEscape(trim($_GET["user"]));
	} else $ReportedPlayer = "";
      
	  if (isset($_POST["submit_report"]) ) {
	     $player = safeEscape(trim($_POST["report_player"]));
		 $subject = safeEscape(trim($_POST["subject"]));
		 $reason = safeEscape(trim($_POST["message"]));
		 $reason = my_nl2br( trim($_POST["message"]) );
		 $reason = nl2br($reason);
		 $reason = EscapeStr( ($reason) );
		 $game_url   = EscapeStr(trim($_POST["game_url"]));
		 $replay_url = EscapeStr(trim($_POST["replay_url"]));
		 $errors = "";
		 
		 if ( strlen($player)<=2 )  $errors.="<div>".$lang["error_report_player"]."</div>";
		 if ( strlen($subject)<=2 ) $errors.="<div>".$lang["error_report_subject"]."</div>";
		 if ( strlen($reason)<=2 )  $errors.="<div>".$lang["error_report_reason"]."</div>";
		 if ( !is_logged() )  $errors ="<div>".$lang["error_report_login"]."</div>";
		 
		 if ( isset($_SESSION["last_report"]) AND $_SESSION["last_report"] + $BanReportTime > time() ) {
		 $TimeLeft = time() - $_SESSION["last_report"];
		 $errors = "<div>".$lang["error_report_time"]." ".($BanReportTime-$TimeLeft)." ".$lang["error_sec"]." </div>";
		 }
		 
		 if ( empty($errors) ) {
		    $check = $db->query("SELECT * FROM ".OSDB_GP." WHERE LOWER(name) = LOWER('".$player."') LIMIT 1");
			if ( $db->num_rows($check)<=0 ) {
			$errors.="<div>".$lang["error_no_player"]."</div>";
			
			$checkBan = $db->query("SELECT * FROM ".OSDB_BANS." WHERE LOWER(name) = LOWER('".$player."') LIMIT 1");
			if ( $db->num_rows($check)>=1 ) {
			$errors.="<div>".$lang["error_already_banned"]."</div>";
			}
			
			} else {
			
			 $find = $db->query("SELECT * FROM ".OSDB_STATS." WHERE LOWER(player) = '".$player."' LIMIT 1 ");
			 if ( $db->num_rows($find)>=1 ) {
			 $row = $db->fetch_array($find,'assoc');
			 $PID = $row["id"];
			 } 
			 else $PID = 0; //Player not ranked yet
			
			 $result = $db->query("INSERT INTO ".OSDB_REPORTS."(player_id, player_name, user_id, reason, game_url, replay_url, added, status, user_ip) VALUES('".$PID."', '".$player."', '".$_SESSION["user_id"]."', '".$reason."', '".$game_url."', '".$replay_url."', '".(int) time()."', '0', '".safeEscape($_SERVER["REMOTE_ADDR"])."')");
			 
			 $_SESSION["last_report"] = time();
	         require_once(OS_PLUGINS_DIR.'index.php');
	         os_init();
			 header('location: '.OS_HOME.'?ban_report&success');
			 
			}

		 }
	  }
?>